import pandas as pd
import numpy as np
import os

path = r'c:\Users\DELL\Desktop\f23016426林泽轩_数据分析可视化作业汇总\作业代码\pyechart\f23016426林泽轩_人口迁移数据.csv'
df = pd.read_csv(path, encoding='utf-8-sig')
df['direction_flow'] = pd.to_numeric(df['direction_flow'], errors='coerce')
df = df.dropna(subset=['direction_flow'])

same_mask = df['start_prov'] == df['end_prov']
mean_same = df.loc[same_mask, 'direction_flow'].mean()
mean_cross = df.loc[~same_mask, 'direction_flow'].mean()
share_same = df.loc[same_mask, 'direction_flow'].sum() / df['direction_flow'].sum() if df['direction_flow'].sum() != 0 else np.nan
ans1 = '是' if (pd.notna(mean_same) and pd.notna(mean_cross) and mean_same > mean_cross) else '否'

city_in = df.groupby('end_city')['direction_flow'].sum()
city_out = df.groupby('start_city')['direction_flow'].sum()

pair = df.groupby(['start_city', 'end_city'])['direction_flow'].sum().reset_index()
pair['start_out_total'] = pair['start_city'].map(city_out)
pair['end_in_total'] = pair['end_city'].map(city_in)
pair['toward_larger'] = pair['end_in_total'] > pair['start_out_total']
sum_larger = pair.loc[pair['toward_larger'], 'direction_flow'].sum()
sum_smaller = pair.loc[~pair['toward_larger'], 'direction_flow'].sum()
mean_larger = pair.loc[pair['toward_larger'], 'direction_flow'].mean()
mean_smaller = pair.loc[~pair['toward_larger'], 'direction_flow'].mean()
share_larger = sum_larger / pair['direction_flow'].sum() if pair['direction_flow'].sum() != 0 else np.nan
ans2 = '是' if sum_larger > sum_smaller else '否'

city_in_sorted = city_in.sort_values(ascending=False)
top_city = city_in_sorted.index[0] if len(city_in_sorted) else None
ans3 = '是' if top_city == '上海' else '否'

print('中国人更倾向于在离家近的城市之间流动吗：', ans1, '同省均值=', round(mean_same if pd.notna(mean_same) else 0, 6), '跨省均值=', round(mean_cross if pd.notna(mean_cross) else 0, 6), '同省占比=', round(share_same if pd.notna(share_same) else 0, 6))
print('中国人更倾向于从中小城市流向更大的中心城市吗：', ans2, '向更大城市的占比=', round(share_larger if pd.notna(share_larger) else 0, 6), '均值对比', round(mean_larger if pd.notna(mean_larger) else 0, 6), '/', round(mean_smaller if pd.notna(mean_smaller) else 0, 6))
print('上海是人口流入最多的城市吗：', ans3, '第一名=', top_city)
print('城市流入Top10')
print(city_in_sorted.head(10).to_string())

out_dir = os.path.dirname(path)
out_file = os.path.join(out_dir, '三大猜想验证结果.txt')
lines = []
lines.append(f"中国人更倾向于在离家近的城市之间流动吗：{ans1} 同省均值={round(mean_same if pd.notna(mean_same) else 0, 6)} 跨省均值={round(mean_cross if pd.notna(mean_cross) else 0, 6)} 同省占比={round(share_same if pd.notna(share_same) else 0, 6)}")
lines.append(f"中国人更倾向于从中小城市流向更大的中心城市吗：{ans2} 向更大城市的占比={round(share_larger if pd.notna(share_larger) else 0, 6)} 均值对比={round(mean_larger if pd.notna(mean_larger) else 0, 6)}/{round(mean_smaller if pd.notna(mean_smaller) else 0, 6)}")
lines.append(f"上海是人口流入最多的城市吗：{ans3} 第一名={top_city}")
lines.append('城市流入Top10')
lines.append(city_in_sorted.head(10).to_string())
with open(out_file, 'w', encoding='utf-8') as f:
    f.write('\n'.join(lines))
print('结果已保存到', out_file)
